<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_DashBoardAction extends m_backoffice_extends_Action
{
	public function execute()
	{
        //get all dashboards
        $dashBoardsArray = m_backoffice_services_DashBoardService::getInstance()->getBoards();

        //get user checked dashboards
        $userDashBoardsArray = $this->getUserContext()->getUser()->getExtraParameter('dashboards');

        $boards = Array();

        if(is_array($userDashBoardsArray))
        {
            foreach($dashBoardsArray as $moduleName => $module)
            {
                foreach($module as $board)
                {
                    if(in_array($moduleName.'_'.$board['title'], $userDashBoardsArray)) //Board still existing
                    {
                        $index = count($boards);
                        $boards[$index]['module'] = $moduleName;
                        $boards[$index]['action'] = $board['action'];
                        $boards[$index]['title'] = $board['title'];
                    }
                }
            }
        }
        //sort $board
        $SortBoards = Array();
        //initialize
        for($i=0;$i<count($boards);$i++)
        {
            $SortBoards[$i] = Array();
        }


        for($i=0;$i<count($boards);$i++)
        {
            $index = 0;
            for($j=0;$j<count($userDashBoardsArray);$j++)
            {
                if($boards[$i]['module'].'_'.$boards[$i]['title'] == $userDashBoardsArray[$j])
                {
                    $SortBoards[$j] = $boards[$i];
                }
            }
        }

        $this->addToModel("boards", $SortBoards);
		$this->setView('success');
	}
}